Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gguf: better type usage #655

Merged
merged 11 commits into from
May 7, 2024
Merged

Conversation

ngxson
Copy link
Member

@ngxson ngxson commented May 4, 2024

Follow up #640

Ref comments:

The type system introduce in this PR allows type-checking at both compile time & runtime:

const model: GGUFMetadata<GGUFType.STRICT> = null as any;

if (model["general.architecture"] === "whisper") {
	model["encoder.whisper.block_count"] = 0;
	// @ts-expect-error because it must be a number
	model["encoder.whisper.block_count"] = "abc";
}

if (model["tokenizer.ggml.model"] === undefined) {
	// @ts-expect-error because it's undefined
	model["tokenizer.ggml.eos_token_id"] = 1;
}
if (model["tokenizer.ggml.model"] === "gpt2") {
	// @ts-expect-error because it must be a number
	model["tokenizer.ggml.eos_token_id"] = undefined;
	model["tokenizer.ggml.eos_token_id"] = 1;
}

if (model["general.architecture"] === "mamba") {
	model["mamba.ssm.conv_kernel"] = 0;
	// @ts-expect-error because it must be a number
	model["mamba.ssm.conv_kernel"] = "abc";
}
if (model["general.architecture"] === "llama") {
	// @ts-expect-error llama does not have ssm.* keys
	model["mamba.ssm.conv_kernel"] = 0;
}

Type checks can be disable with GGUFMetadata<GGUFType.NON_STRICT>

@ngxson
Copy link
Member Author

ngxson commented May 4, 2024

Also in the process, I noticed some details not being mentioned in gguf specs:

@mishig25 mishig25 requested a review from coyotte508 May 6, 2024 08:06
packages/gguf/src/types.ts Outdated Show resolved Hide resolved
packages/gguf/src/types.ts Outdated Show resolved Hide resolved
@mishig25
Copy link
Collaborator

mishig25 commented May 6, 2024

Very nice PR! And much needed improvement to the typing of gguf, especially type inference.
Left some nits. Otherwise, we are close to merge 🚀

Partial<Model> &
Record<string, MetadataValue>;
} & GGUFModelKV &
(TGGUFType extends GGUFType.strict ? unknown : Record<string, MetadataValue>);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, the unknown here allow casting between GGUFParseOutput<GGUFType.strict> and GGUFParseOutput<GGUFType.nonStrict>. Funny though, I have no idea why adding unknown make it work. Maybe this is not the best way to do, so feel free to tell if you have other suggestions.

packages/gguf/src/types.ts Outdated Show resolved Hide resolved
Copy link
Member

@julien-c julien-c left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks in good shape! do you want to take a quick look too @coyotte508?

@mishig25
Copy link
Collaborator

mishig25 commented May 6, 2024

Question regarding GGUFType.NON_STRICT. Why do we need GGUFType.NON_STRICT? Shouldn't we want GGUFType.STRICT to be always the case?

@ngxson
Copy link
Member Author

ngxson commented May 6, 2024

I believe that GGUFType.NON_STRICT will be useful in case user want to use their own gguf structure. In fact, gguf can also be used for storing control vectors, lora weights, and maybe other use cases outside of llama.cpp

Copy link
Collaborator

@mishig25 mishig25 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM !

@mishig25
Copy link
Collaborator

mishig25 commented May 7, 2024

@coyotte508 could you give a quick look and I will merge 🚀

Copy link
Member

@coyotte508 coyotte508 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

I'm wondering about flat paths vs nested metadata (maybe it could be another option in the future 🤔) but LGTM with the 1/2 changes requested

@@ -308,6 +308,9 @@ export async function gguf(
}
}
offset += valueResult.length;
/// TODO(fix typing)
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use // @ts-expect-error instead of // @ts-ignore in general

(no need for eslint-disable this way)

Here I think you can change const metadata: GGUFMetadata to const metadata: GGUFMetadata<GGUFType.NON_STRICT> to remove the error (not sure if it's the best fix)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 31bac8b

Comment on lines 113 to 118
export enum GGUFType {
STRICT,
NON_STRICT,
}

export type GGUFMetadata<TGGUFType extends GGUFType = GGUFType.STRICT> = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably switch to something like https://github.com/sindresorhus/type-fest/blob/main/source/except.d.ts

interface GGUFMetadataOptions {
	/**
   * ...
   * 
	 * @default true
   */
  strict: boolean;
}


export GGUFMetadata<Options extends GGUFMetadataOptions = { strict: true}> {
  ...

Copy link
Member Author

@ngxson ngxson May 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea 👍 I've never thought about this before. Implemented in 31bac8b

@ngxson
Copy link
Member Author

ngxson commented May 7, 2024

I'm wondering about flat paths vs nested metadata (maybe it could be another option in the future 🤔)

@coyotte508 I thought about using nested object, but that seems to be quite complicated atm, since part of the code/naming here is copied directly from cpp file (so it save us some headaches)

But I'm also agree that having nested object is a reasonable feature, since gguf keys are "namespaced" anyway. Maybe we will re-visit this idea later on.

@julien-c
Copy link
Member

julien-c commented May 7, 2024

alright let's merge!

re. nested properties, that's what they did in https://github.com/ahoylabs/gguf.js (credited as one of the inspirations for this package) but I'm not sure it's worth it / I find it cleaner to just expose the raw data from the GGUF file.

@julien-c julien-c merged commit 6a036d8 into huggingface:main May 7, 2024
3 of 4 checks passed
@ngxson ngxson mentioned this pull request May 12, 2024
mishig25 pushed a commit that referenced this pull request May 13, 2024
Follow up #655 and
#656 (comment)

Added some examples on how to use local file + strictly typed

---------

Co-authored-by: Julien Chaumond <[email protected]>
Co-authored-by: Mishig <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants